package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.JtCountryCode;
import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtProductCategory;
import com.ruoyi.business.service.IJtProductCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品分类Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/business/category")
public class JtProductCategoryController extends BaseController
{
    private String prefix = "business/category";

    @Autowired
    private IJtProductCategoryService jtProductCategoryService;

    @RequiresPermissions("business:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("business:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtProductCategory jtProductCategory)
    {
        String categoryName = (String) jtProductCategory.getParams().get("categoryName");

        Object langObj = jtProductCategory.getParams().get("lang");
        if("zh".equals(langObj)) {
            jtProductCategory.setCategoryNameZh(categoryName);
        } else if("en".equals(langObj)) {
            jtProductCategory.setCategoryNameEn(categoryName);
        } else if("ar".equals(langObj)) {
            jtProductCategory.setCategoryNameAr(categoryName);
        }
        startPage();
        List<JtProductCategory> list = jtProductCategoryService.selectJtProductCategoryList(jtProductCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("business:category:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtProductCategory jtProductCategory)
    {
        List<JtProductCategory> list = jtProductCategoryService.selectJtProductCategoryList(jtProductCategory);
        ExcelUtil<JtProductCategory> util = new ExcelUtil<JtProductCategory>(JtProductCategory.class);
        return util.exportExcel(list, "商品分类数据");
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("business:category:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品分类
     */
    @RequiresPermissions("business:category:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtProductCategory jtProductCategory)
    {
        return toAjax(jtProductCategoryService.insertJtProductCategory(jtProductCategory));
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("business:category:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap)
    {
        JtProductCategory jtProductCategory = jtProductCategoryService.selectJtProductCategoryByCategoryId(categoryId);
        mmap.put("jtProductCategory", jtProductCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分类
     */
    @RequiresPermissions("business:category:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtProductCategory jtProductCategory)
    {
        return toAjax(jtProductCategoryService.updateJtProductCategory(jtProductCategory));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("business:category:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtProductCategoryService.deleteJtProductCategoryByCategoryIds(ids));
    }

    /**
     * 查询分类列表
     */
    @GetMapping("/select/list")
    @ResponseBody
    public AjaxResult selectList()
    {
        JtProductCategory param = new JtProductCategory();
        param.setStatus(0);
        List<JtProductCategory> list = jtProductCategoryService.selectJtProductCategoryList(param);

        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", list);
        return ajax;
    }

}
